home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / processes / howtohideyourapp / howtohideapp.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.6 KB  |  211 lines

  1. /*    File:        HowToHideApp.c
  2.     
  3.     Description: 
  4.              This sample illustrates how the SetHideOnSwitch and GetHideOnSwitch
  5.             routines can be used to hide an application.  These routines were first
  6.             documented in Technote TN1102, "Mac OS 8".  On the world wide
  7.             web, documentation for these routines can be found at the address:
  8.             
  9.             http://developer.apple.com/technotes/tn/tn1102.html#processmgr
  10.             
  11.             This file contains a sample application that calls the routines defined
  12.             in HideCalls.h to hide itself.
  13.  
  14.     Copyright: 
  15.             Copyright © 1999 by Apple Computer, Inc.
  16.             All rights reserved.
  17.     
  18.     Disclaimer:
  19.             You may incorporate this sample code into your applications without
  20.             restriction, though the sample code has been provided "AS IS" and the
  21.             responsibility for its operation is 100% yours.  However, what you are
  22.             not permitted to do is to redistribute the source as "DSC Sample Code"
  23.             after having made changes. If you're going to re-distribute the source,
  24.             we require that you make it clear in the source that the code was
  25.             descended from Apple Sample Code, but that you've made changes.
  26.     
  27.     Change History (most recent first):
  28.             12/6/1999 created
  29. */
  30.  
  31. #include <Types.h>
  32. #include <QuickDraw.h>
  33. #include <Menus.h>
  34. #include <Windows.h>
  35. #include <Dialogs.h>
  36. #include <Events.h>
  37. #include <Fonts.h>
  38. #include <SegLoad.h>
  39. #include <Resources.h>
  40. #include <Gestalt.h>
  41. #include <Appearance.h>
  42.  
  43. #include "HideCalls.h"
  44.  
  45.     /* true while the app is running */
  46. Boolean gRunning = true;
  47.  
  48.     /* true while the app is in forground */
  49. Boolean gForground = true;
  50.  
  51.     /* true if appearance exists */
  52. Boolean gAppearance = false;
  53.  
  54.     /* the main window */
  55. DialogPtr gMainWindow;
  56.  
  57.     /* the 'hide' button */
  58. ControlHandle gHideButton;
  59.  
  60.     /* QuickDraw globals*/
  61. #ifndef __MWERKS__
  62. QDGlobals    qd;
  63. #endif
  64.  
  65.  
  66. /* MyChooseP is a callback routine that the HowToHideApp provides as a
  67.     parameter to the HideMe routine.  Currently, this routine selects
  68.     the Finder as the next process to be switched into the forground
  69.     when the HowToHideApp is hidden. */
  70. static OSStatus MyChooseP(ProcessSerialNumber *myPSN, ProcessSerialNumber *targetPSN) {
  71.     ProcessInfoRec pInfo;
  72.     ProcessSerialNumber iteratingPSN;
  73.     OSStatus err;
  74.         /* iterate through all active processes */
  75.     iteratingPSN.highLongOfPSN = iteratingPSN.lowLongOfPSN = kNoProcess;
  76.     while (GetNextProcess(&iteratingPSN) == noErr) {
  77.             /* get the process information */
  78.         pInfo.processInfoLength = sizeof(pInfo);
  79.         pInfo.processName = NULL;
  80.         pInfo.processAppSpec = NULL;
  81.         err = GetProcessInformation(&iteratingPSN, &pInfo);
  82.         if (err != noErr) return err;
  83.             /* if it's the Finder, return its process serial number */
  84.         if (pInfo.processSignature == 'MACS') {
  85.             *targetPSN = iteratingPSN;
  86.             return noErr;
  87.         }
  88.     }
  89.         /* Finder not found */
  90.     return procNotFound;
  91. }
  92.  
  93.  
  94. /* MyEventHandler is the main event handler for the HowToHideApp
  95.     application.  It is called for every event returned by
  96.     WaitNextEvent, and it is also provided as a parameter to the
  97.     HideMe routine when the HowToHideApp is being hidden. Basically,
  98.     this routine draws the dialog and calls HideMe whenever 
  99.     the "Hide Me Now" button is clicked.  */
  100. static OSStatus MyEventHandler(EventRecord *ev) {
  101.     OSStatus err;
  102.         /* track forground switches */
  103.     if ((ev->what == osEvt) && (((ev->message >> 24) & 0x0FF) == suspendResumeMessage)) {
  104.         gForground = ((ev->message & resumeFlag) != 0);
  105.         DrawDialog(gMainWindow);
  106.         HiliteControl(gHideButton, gForground ? 0 : 255);
  107.     }
  108.         /* redraw if we're activating */
  109.     if (ev->what == activateEvt) {
  110.         DrawDialog(gMainWindow);
  111.         HiliteControl(gHideButton, ((ev->modifiers & activeFlag) != 0) ? 0 : 255);
  112.     }
  113.         /* handle clicks in the dialog window */
  114.     if (IsDialogEvent(ev)) {
  115.         DialogPtr theDialog;
  116.         short itemNo;
  117.         if (DialogSelect(ev, &theDialog, &itemNo)) {
  118.         
  119.             HiliteControl(gHideButton, 255); /* HideMe is not re-entrant */
  120.             
  121.                 /* hide the application */
  122.             err = HideMe(MyEventHandler, MyChooseP);
  123.             
  124.         }
  125.     }
  126.         /* process other window commands */
  127.     if (ev->what == mouseDown) {
  128.         WindowPtr theWindow;
  129.         switch (FindWindow(ev->where, &theWindow)) {
  130.                 
  131.                 /* clicks in the close box, close the app */
  132.             case inGoAway:
  133.                 if (TrackGoAway(theWindow, ev->where))
  134.                     gRunning = false;
  135.                 break;
  136.                 
  137.                 /* allow window drags */
  138.             case inDrag:
  139.                 {    Rect boundsRect = { -32000, -32000, 32000, 32000};
  140.                     DragWindow(theWindow, ev->where, &boundsRect);
  141.                 }
  142.                 break;
  143.                 
  144.                 /* desktop clicks, etc... */
  145.             case inSysWindow:
  146.                 SystemClick(ev, theWindow);
  147.                 break;
  148.         }
  149.     }
  150.         /* if we're not running, return userCanceledErr.  This allows
  151.         the user to abort the HideMe by closing the window if it gets stuck. */
  152.     if (gRunning)
  153.         return noErr;
  154.     else return userCanceledErr;
  155. }
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. int main(void) {
  166.     OSErr err;
  167.     long response;
  168.         /* set up our app */
  169.     SetApplLimit(GetApplLimit());
  170.     MaxApplZone();
  171.     InitGraf(&qd.thePort);
  172.     InitFonts();
  173.     InitWindows();
  174.     TEInit();
  175.     InitMenus();
  176.     InitDialogs(0);
  177.     FlushEvents(everyEvent, 0);
  178.     InitCursor();
  179.     if (Gestalt(gestaltAppearanceAttr, &response) != noErr) response = 0;
  180.     if ((response & (1<<gestaltAppearanceExists)) != 0) {
  181.         err = RegisterAppearanceClient();
  182.         if (err != noErr) goto bail;
  183.         gAppearance = true;
  184.     }
  185.     
  186.         /* set up the dialog */
  187.     gMainWindow = GetNewDialog(128, NULL, (WindowPtr) (-1));
  188.     {    short itemt;
  189.         Rect itemb;
  190.         GetDialogItem(gMainWindow, 1, &itemt, (Handle*) &gHideButton, &itemb);
  191.     }
  192.  
  193.         /* run the app */
  194.     while (gRunning) {
  195.         EventRecord ev;
  196.             
  197.             /* get the next event */
  198.         if ( ! WaitNextEvent(everyEvent, &ev,  GetCaretTime(), NULL))
  199.             ev.what = nullEvent;
  200.             
  201.             /* pass the event along to the event handler */
  202.         err = MyEventHandler(&ev);
  203.         if (err != noErr) goto bail;
  204.     }
  205.     
  206. bail:
  207.     if (gAppearance)
  208.         UnregisterAppearanceClient();
  209.     ExitToShell();
  210.     return 0;
  211. }